import Vue from 'vue'
import Vuex from 'vuex'
import { handleFlat } from '@/utils'

Vue.use(Vuex)

const modules = {}
const files = require.context('./modules', true, /\.js$/)
files.keys().forEach(path => {
  let module = files(path).default
  let name = /'\.\/(.*?)\./.exec(`"'${path}'"`)[1]
  modules[name] = module
})

export default new Vuex.Store({
  state: {
    loading: false
  },
  getters: {
    permissionList(state) {
      let { navList } = state.app
      if (navList.length > 0) {
        return handleFlat(navList)
      } else {
        return navList
      }
    }
  },
  mutations: {
    SET_LOADING(state, loading) {
      state.loading = loading
    }
  },
  actions: {
    /**
     * layout布局内请求loading
     */
    SET_LOADING({ commit }, loading) {
      commit('SET_LOADING', loading)
    }
  },
  modules
})
